//
// winter - Direct2D GUI framework.
// Copyright (C) 2009 Aleksandar Dezelin 
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#ifndef THREAD_SINGLETON_H
#define THREAD_SINGLETON_H

#pragma once
#include "noncopyable.h"
#include "thread_singleton_host.h"

namespace winter {
namespace services {

/// <summary>
///     Implements per-thread singleton.
/// </summmary>
template<class T> class thread_singleton : public noncopyable {
public:
  typedef thread_singleton<T> this_type;

  /// <summary>
  ///     Returns and initializes the singleton instance on first call.
  /// </summary>
  /// <returns>
  ///     The singleton instance.
  /// </returns>
  static T* instance() {
    return platform::thread_singleton_host<T>::instance();
  }

  /// <summary>
  ///     Destroys the singleton instance.
  ///     NOTE: not thread safe.
  /// </summary>
  static void destroy() {
    platform::thread_singleton_host<T>::destroy();
  }

private:
  explicit thread_singleton();
  ~thread_singleton();
};

}
}

#endif // THREAD_SINGLETON_H
